package server.resources;

import java.util.Arrays;
import java.util.Map;

import json.JSONObjectWrapper;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import server.database.DatabaseColumn;
import server.database.DatabaseService;
import server.database.query.IQueryResult;
import server.servlet.AbstractEntityServlet;

public class ProductsServlet extends AbstractEntityServlet
{
	private static final long serialVersionUID = 8998888023324244993L;

	public static final String PATH = API + "/products";

	public static final String PRODUCT = "product";
	public static final String PRODUCTS = "products";

	public static final String CRANES = "cranes";
	public static final String REMOVED_CRANES = "removed_cranes";

	@Override
	protected String getEntityName()
	{
		return PRODUCT;
	}

	@Override
	protected String getEntitiesName()
	{
		return PRODUCTS;
	}

	@Override
	protected ProductsTable getTable()
	{
		return (ProductsTable) DatabaseService.getInstance().getTable(ProductsTable.TABLE_NAME);
	}

	protected ProductCranesTable getProductCranesTable()
	{
		return (ProductCranesTable) DatabaseService.getInstance().getTable(ProductCranesTable.TABLE_NAME);
	}

	@Override
	protected boolean addMultipleTables(JSONObjectWrapper entity, int iID)
	{
		JSONArray cranes = entity.getJsonArray(CRANES);
		for (Object craneObject : cranes)
		{
			JSONObjectWrapper crane = new JSONObjectWrapper((JSONObject) craneObject);
			Map<DatabaseColumn, String> mpCraneValues = parseEntityValues(crane, getProductCranesTable());
			mpCraneValues.put(ProductCranesTable.PRODUCT_ID, String.valueOf(iID));

			getProductCranesTable().insert(mpCraneValues);
			// TODO - need to remove all lines on failure
		}

		return true;
	}

	@Override
	protected void getMultipleTables(JSONArray entities)
	{
		ProductCranesTable productCranes = getProductCranesTable();

		for (Object productObject : entities)
		{
			JSONObjectWrapper product = new JSONObjectWrapper((JSONObject) productObject);

			IQueryResult productCranesQuery = productCranes.getAllEntities(Arrays.asList(ProductCranesTable.PRODUCT_ID), Arrays.asList(product.getStringValue(getTable().ID.getName())), getTable().AND);
			if (productCranesQuery.getResult() == IQueryResult.SUCESS)
			{
				JSONArray cranes = new JSONArray();
				buildEntityValues(productCranesQuery.getResultValues(), cranes, productCranes);
				product.putValue(CRANES, cranes);
			}

		}
	}

	@Override
	protected boolean updateMultipleTables(JSONObjectWrapper entity, String sID)
	{
		ProductCranesTable productCranes = getProductCranesTable();
		JSONArray cranes = entity.getJsonArray(CRANES);

		for (Object craneObject : cranes)
		{
			JSONObjectWrapper crane = new JSONObjectWrapper((JSONObject) craneObject);
			Map<DatabaseColumn, String> mpCraneValues = parseEntityValues(crane, productCranes);
			mpCraneValues.put(ProductCranesTable.PRODUCT_ID, sID);
			productCranes.insert(mpCraneValues);
		}

		JSONArray removedCranes = entity.getJsonArray(REMOVED_CRANES);
		for (Object craneObject : removedCranes)
		{
			JSONObjectWrapper crane = new JSONObjectWrapper((JSONObject) craneObject);
			String sCraneID = crane.getStringValue(ProductCranesTable.CRANE_ID.getName());

			productCranes.delete(Arrays.asList(ProductCranesTable.PRODUCT_ID, ProductCranesTable.CRANE_ID), Arrays.asList(sID, sCraneID));
		}

		return true;
	}

	@Override
	protected boolean deleteMultipleTables(String sID)
	{
		ProductCranesTable productCranes = getProductCranesTable();
		productCranes.delete(Arrays.asList(ProductCranesTable.PRODUCT_ID), Arrays.asList(sID));

		return true;
	}

}
